package auctionhaus

import grails.test.*
import com.auctionhaus.Customer
import com.auctionhaus.Listing
import grails.converters.*

class ListingServiceIntegrationTests extends GroovyTestCase {
    def ListingService
    def OnNotificationService
    def jmsService

    protected void setUp() {
        super.setUp()
        new Customer(emailAddress: "lady@gaga.com", password: "password", date:new Date()).save()
    }

    protected void tearDown() {
        super.tearDown()
    }
    //S-2: Create a Grails service method that supports creating a new listing (integration test)
    void testCreateListing() {
        def listing = ListingService.createListing("lady@gaga.com", "password", "Poker Face Poster", new Date() + 5, 10.00, "Original Replica")
        def foundListing = Listing.findByListName("Poker Face Poster")
        assertEquals listing.listName, foundListing.listName
        assertEquals listing.seller.emailAddress, "lady@gaga.com"
    }
    //S-4: The Grails service method for S-2 must validate the sellers username and password before creating the listing (integration test)
     void testCreateListingWithInvalidUserName() {
        //incorrect
        def listing = ListingService.createListing("notlady@gaga.com", "password", "Just Dance", new Date() + 5, 10.00, "Sweet Moves for Sweet Grooves")
        def foundListing = Listing.findByListName("Just Dance")
        assertNull listing
        assertNull foundListing

        //corrected
        listing = ListingService.createListing("lady@gaga.com", "password", "Just Dance", new Date() + 5, 10.00, "Sweet Moves for Sweet Grooves")
        foundListing = Listing.findByListName("Just Dance")
        assertEquals listing, foundListing

        //check case insensitive on email Address
        listing = ListingService.createListing("LaDy@gaga.com", "password", "Just Dance", new Date() + 5, 10.00, "Sweet Moves for Sweet Grooves")
        assertNotNull listing
    }
    void testCreateListingWithInvalidPassword() {
        //incorrect password
        def listing = ListingService.createListing("lady@gaga.com", "incorrect", "Just Dance", new Date() + 5, 10.00, "Sweet Moves for Sweet Grooves")
        def foundListing = Listing.findByListName("Just Dance")
        assertNull listing
        assertNull foundListing

        //corrected password, case insensitive emailAddress
        listing = ListingService.createListing("LaDy@gaga.com", "password", "Just Dance", new Date() + 5, 10.00, "Sweet Moves for Sweet Grooves")
        assertNotNull listing

        //testing case sensitive password
        listing = ListingService.createListing("lady@gaga.com", "PaSsWoRd", "Just Dance", new Date() + 5, 10.00, "Sweet Moves for Sweet Grooves")
        assertNull listing
    }


    // C-2: When a listing completes post a message to a JMS Queue representing sending the completed listing
    // information including listing details, seller and winner information as XML or JSON (integration test)
    void testPostMessageAfterListingCompletes() {

        def listing = ListingService.createListing("lady@gaga.com", "password", "Just Dance", new Date(System.currentTimeMillis() + 999), 20.00, "Description of Album")
        def foundListing = Listing.findByListName("Just Dance")
        assertNotNull foundListing
        Thread.sleep(1000)

        ListingService.doListingExpired()
        msg = jmsService.receiveSelectedJMSMessage(queue: "queue.notification", "", 5000, "standard")
        //List messages = jmsService.browseNoConvert("queue.notification")
        assertNotNull msg
        println msg

    }

     void testCreateListingFromXMLMessage() {
         def customerSeller = new Customer (emailAddress: "seller@gmail.com", password: "abcdefg", date:new Date()).save()

         def l = new Listing(creationDate:new Date(), listName:"XML created Listing",endDateTime:new Date() + 5, startBidPrice:100, seller:customerSeller)
         def message = l as XML

         ListingService.sendJMSMessage("queue.listing.listener", message.toString())
         ListingService.onMessage(message.toString())

         def foundListing = Listing.findByListName("XML created Listing")
         assertNotNull foundListing
    }
}
